<?php
define('MYTHUMBNAIL_BASE_PATH', dirname(__FILE__));
//define('MYTHUMBNAIL_DEFAULT_IMPLEMENTATION', 'gd');
require_once 'myThumbnail/myThumbnail_gd.implementation.php';
class myThumbnail
{
	private $implementation;
	static private $myThumbnail_gd_implementation = false;
	public function __construct ($fileName, $options = array(), $isDataStream = false)
	{
		switch (true)
		{
			case file_exists(MYTHUMBNAIL_BASE_PATH.'/myThumbnail/myThumbnail_gd.implementation.php'):
				$instance_name = 'myThumbnail_gd';				
				break;
			default:
				throw new Exception('No existe la clases que las imagenes como GD');
				
		}
		$this->implementation = $instance_name::newInstance($fileName,$options,$isDataStream);
	}
	/**
	 * create from Image or create Dinamic
	 * @param string $fileName
	 * @param array $options
	 * @param bool $isDataStream
	 * @return myThumbnail
	 */
	public static function newInstance($fileName, $options = array(), $isDataStream = false)
	{
		return new self($fileName, $options , $isDataStream );
	}
	/**
	 * Class Destructor
	 * 
	 */
	public function __destruct ()
	{
		unset($this->implementation);
	}
	/**
	 * Enter description here ...
	 * @param int $maxWidth
	 * @param int $maxHeight
	 * @return myThumbnail
	 */
	public function resize ($maxWidth = 0, $maxHeight = 0)
	{
		return $this->implementation->resize($maxWidth,$maxHeight);
	}
	/**
	 * Adaptively Resizes the Image
	 * 
	 * This function attempts to get the image to as close to the provided dimensions as possible, and then crops the 
	 * remaining overflow (from the center) to get the image to be the size specified
	 * 
	 * @param int $maxWidth
	 * @param int $maxHeight
	 * @return GdThumb
	 */
	public function adaptiveResize ($width, $height)
	{
		return $this->implementation->adaptiveResize ($width, $height);
	}
	/**
	 * Resizes an image by a given percent uniformly
	 * 
	 * Percentage should be whole number representation (i.e. 1-100)
	 * 
	 * @param int $percent
	 * @return GdThumb
	 */
	public function resizePercent ($percent = 0)
	{
		return $this->implementation->resizePercent ($percent);
	}
	/**
	 * Crops an image from the center with provided dimensions
	 * 
	 * If no height is given, the width will be used as a height, thus creating a square crop
	 * 
	 * @param int $cropWidth
	 * @param int $cropHeight
	 * @return GdThumb
	 */
	public function cropFromCenter ($cropWidth, $cropHeight = null)
	{
		return $this->implementation->cropFromCenter ($cropWidth, $cropHeight);
	}
	/**
	 * Vanilla Cropping - Crops from x,y with specified width and height
	 * 
	 * @param int $startX
	 * @param int $startY
	 * @param int $cropWidth
	 * @param int $cropHeight
	 * @return GdThumb
	 */
	public function crop ($startX, $startY, $cropWidth, $cropHeight)
	{
		return $this->implementation->crop ($startX, $startY, $cropWidth, $cropHeight);
	}
	/**
	 * Rotates image either 90 degrees clockwise or counter-clockwise
	 * 
	 * @param string $direction
	 * @retunrn GdThumb
	 */
	public function rotateImage ($direction = 'CW',$bgcolor = null)
	{
		return $this->implementation->rotateImage ($direction,$bgcolor );
	}
	/**
	 * Rotates image specified number of degrees
	 * 
	 * @param int $degrees
	 * @return myThumbnail
	 */
	public function rotateImageNDegrees ($degrees,$bgcolor = null)
	{
		return $this->implementation->rotateImageNDegrees ($degrees,$bgcolor);
	}
	/**
	 * Shows an image
	 * 
	 * This function will show the current image by first sending the appropriate header
	 * for the format, and then outputting the image data. If headers have already been sent, 
	 * a runtime exception will be thrown 
	 * 
	 * @param bool $rawData Whether or not the raw image stream should be output
	 * @return GdThumb
	 */
	public function show ($rawData = false)
	{
		return $this->implementation->show ($rawData );
	}
	/**
	 * Returns the Working Image as a String
	 *
	 * This function is useful for getting the raw image data as a string for storage in
	 * a database, or other similar things.
	 *
	 * @return string
	 */
	public function getImageAsString ()
	{
		return $this->implementation->getImageAsString ();
	}
	/**
	 * Saves an image
	 * 
	 * This function will make sure the target directory is writeable, and then save the image.
	 * 
	 * If the target directory is not writeable, the function will try to correct the permissions (if allowed, this
	 * is set as an option ($this->options['correctPermissions']).  If the target cannot be made writeable, then a
	 * RuntimeException is thrown.
	 * 
	 * TODO: Create additional paramter for color matte when saving images with alpha to non-alpha formats (i.e. PNG => JPG)
	 * 
	 * @param string $fileName The full path and filename of the image to save
	 * @param string $format The format to save the image in (optional, must be one of [GIF,JPG,PNG]
	 * @return GdThumb
	 */
	public function save ($fileName, $format = null)
	{
		return $this->implementation->save ($fileName, $format);
	}
	/**
	 * Enter description here ...
	 * @param int $percent
	 * @param int $reflection
	 * @param int $white
	 * @param bool $border
	 * @param string $borderColor example:#FFFFFF
	 * @return myThumbnail
	 */
	public function createReflection ($percent, $reflection, $white, $border, $borderColor)
	{
		return $this->implementation->createReflection ($percent, $reflection, $white, $border, $borderColor);
	}
	/**
	 * Enter description here ...
	 * @param resource $resource
	 * @param int $x_pos
	 * @param int $y_pos
	 * @param int $opacity_0_100
	 * @return myThumbnail
	 */
	public function merge($resource,$x_pos,$y_pos,$opacity_0_100 = 100)
	{
		return $this->implementation->merge($resource,$x_pos,$y_pos,$opacity_0_100);
	}
	public function createText($text,$size,$x_pos,$y_pos,$color = '#000000',$alpha = 0)
	{
		return $this->implementation->createText($text,$size,$x_pos,$y_pos,$color,$alpha);
	}
	/**
	 * Enter description here ...
	 * @param int $x_pos
	 * @param int $y_pos
	 * @param int $width
	 * @param int $height
	 * @param string $color
	 * @param int $alpha
	 * @return myThumbnail
	 */
	public function createRectangle($x_pos,$y_pos,$width, $height,$color = '#FFFFFF',$alpha = 0)
	{
		return $this->implementation->createRectangle($x_pos,$y_pos,$width, $height,$color ,$alpha);
	}
	#################################
	# ----- GETTERS / SETTERS ----- #
	#################################
	
	/**
	 * Sets $this->options to $options
	 * 
	 * @param array $options
	 */
	public function setOptions ($options = array())
	{
		return $this->implementation->setOptions ($options);
	}
	/**
	 * Returns $currentDimensions.
	 *
	 * @see GdThumb::$currentDimensions
	 */
	public function getCurrentDimensions()
	{
		return $this->implementation->getCurrentDimensions();
	}
	/**
	 * Sets $currentDimensions.
	 *
	 * @param object $currentDimensions
	 * @see GdThumb::$currentDimensions
	 */
	public function setCurrentDimensions ($currentDimensions)
	{
		return $this->implementation->setCurrentDimensions ($currentDimensions);
	}
	/**
	 * Returns $maxHeight.
	 *
	 * @see GdThumb::$maxHeight
	 */
	public function getMaxHeight()
	{
		return $this->implementation->getMaxHeight();
	}
	/**
	 * Sets $maxHeight.
	 *
	 * @param object $maxHeight
	 * @see GdThumb::$maxHeight
	 */
	public function setMaxHeight ($maxHeight)
	{
		return $this->implementation->setMaxHeight ($maxHeight);
	}
	/**
	 * Returns $maxWidth.
	 *
	 * @see GdThumb::$maxWidth
	 */
	public function getMaxWidth()
	{
		return $this->implementation->getMaxWidth();
	}
	/**
	 * Sets $maxWidth.
	 *
	 * @param object $maxWidth
	 * @see GdThumb::$maxWidth
	 */
	public function setMaxWidth($maxWidth)
	{
		return $this->implementation->setMaxWidth($maxWidth);
	}
	/**
	 * Returns $newDimensions.
	 *
	 * @see GdThumb::$newDimensions
	 */
	public function getNewDimensions()
	{
		return $this->implementation->getNewDimensions();
	}
	/**
	 * Sets $newDimensions.
	 *
	 * @param object $newDimensions
	 * @see GdThumb::$newDimensions
	 */
	public function setNewDimensions($newDimensions)
	{
		return $this->implementation->setNewDimensions($newDimensions);
	}
	/**
	 * Returns $options.
	 *
	 * @see GdThumb::$options
	 */
	public function getOptions()
	{
		return $this->implementation->getOptions();
	}
	/**
	 * Returns $percent.
	 *
	 * @see GdThumb::$percent
	 */
	public function getPercent()
	{
		return $this->implementation->getPercent();
	}
	/**
	 * Sets $percent.
	 *
	 * @param object $percent
	 * @see GdThumb::$percent
	 */
	public function setPercent($percent)
	{
		return $this->implementation->setPercent($percent);
	}
	/**
	 * Returns $oldImage.
	 *
	 * @see GdThumb::$oldImage
	 */
	public function getOldImage()
	{
		return $this->implementation->getOldImage();
	}
	
	/**
	 * Sets $oldImage.
	 *
	 * @param object $oldImage
	 * @see GdThumb::$oldImage
	 */
	public function setOldImage($oldImage)
	{
		return $this->implementation->setOldImage($oldImage);
	}
	/**
	 * Returns $workingImage.
	 *
	 * @see GdThumb::$workingImage
	 */
	public function getWorkingImage()
	{
		return $this->implementation->getWorkingImage();
	}
	/**
	 * Sets $workingImage.
	 *
	 * @param object $workingImage
	 * @see GdThumb::$workingImage
	 */
	public function setWorkingImage($workingImage)
	{
		return $this->implementation->setWorkingImage($workingImage);
	}
}